package com.swcw.platform.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

/**
 * 普通类获取数据库操作bean
 *
 * @author 杨坚
 * @version [版本号, 2016年2月19日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ServiceLocator implements BeanFactoryAware {  
    private static BeanFactory beanFactory = null;  
    
    private static ServiceLocator servlocator = null;  
   
    @SuppressWarnings("static-access")
	public void setBeanFactory(BeanFactory factory) throws BeansException {  
        this.beanFactory = factory;  
    }  
   
    public BeanFactory getBeanFactory() {  
        return beanFactory;  
    }  
   
     
    public static ServiceLocator getInstance() {  
        if (servlocator == null)  
              servlocator = (ServiceLocator) beanFactory.getBean("serviceLocator");  
        return servlocator;  
    }  
   
    /** 
    * 根据提供的bean名称得到相应的服务类      
    * @param servName bean名称      
    */  
    public static Object getService(String servName) {  
        return beanFactory.getBean(servName);  
    }  
   
    /** 
    * 根据提供的bean名称得到对应于指定类型的服务类 
    * @param servName bean名称 
    * @param clazz 返回的bean类型,若类型不匹配,将抛出异常 
    */  
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object getService(String servName, Class clazz) {  
        return beanFactory.getBean(servName, clazz);  
    }  
}  